<?php
require_once dirname(__FILE__) . "/constants.php";

$output = array("success"=>true, "output"=>array());

/**
 * Echo empty if no suitable method
 */
function empty_output(){
    echo json_encode($output);
}

/**
 * Try to get redis instance
 *
 * @return Redis
 */
function getRedis(){
    try{
        $redis = new Redis();
        $redis->pconnect(REDIS_SERVER, REDIS_PORT);
        return $redis;
    }catch(Exception $e){
        return null;
    }
}

/**
 * Get content by key from Redis
 *
 * @param string $key
 * @return string
 */
function getRedisContentByKey($key){
    $redis = getRedis();
    if($redis){
        return $redis->get($key);
    }else{
        return null;
    }
}

/**
 * Set content by key to Redis
 * 
 * @param string $key
 * @param string $value
 */
function setRedisContentByKey($key, $value){
    $redis = getRedis();
    if($redis){
        $redis->set($key, $value);
    }
}

?>
